<?php

/**
 * 通用控制器
 *
 * @author cdlei
 */

class Admin_CommonController extends Zend_Controller_Action
{
     /**
     * 当前用户记录
     *
     * @var array
     */
    protected $_currentUserRow;

    /**
     * 模型
     *
     * @var object
     */

    protected $_model;

    /**
     * 系统模型对象
     *
     * @var object
     */
    protected $_cookieModel;

    /**
     * 是否需要检查登陆
     *
     * @var integer
     */
    protected $_loginIsCheck = 1 ;

    /**
     * 当前用户记录
     *
     * @var array
     */
    protected $_configs = NULL;

    /**
     * 初始化
     * 
     * @return void
     */
    public function init()
    {
        header("content-type:text/html; charset=utf-8");
        $viewRenderer = Zend_Controller_Action_HelperBroker::getStaticHelper('viewRenderer');
        $viewRenderer->setView($this->view)->setViewSuffix('php');

        $this->_configs = Zend_Registry::get('configs');

        $this->view->baseUrl = $this->_configs['project']['baseUrl'];
        $this->view->systemName = $this->_configs['project']['systemName'];
        $this->view->module = $this->_request->getModuleName();
        $this->view->controller = $this->_request->getControllerName();
        $this->view->action = $this->_request->getActionName();
        $this->view->master = $this->_configs['project']['admin']['master'];

        $this->_cookieModel = new Application_Model_Cookie();
        //如果需要检查登录
        if ($this->_loginIsCheck) {
            $this->_loginCheckInit();
        }

        define('TIME', time());
        if (date('H', TIME) == 2) {
            echo $this->view->render('maintenance.php');
            exit;
        }

    }

    protected function _loginCheckInit()
    {
        $userLoginModel = Application_Model_UserLogin::getInstance();
        $userLoginModel->setTimeToLive($this->_configs['project']['admin']['loginTimeToLive']);
        $loginStatus = $userLoginModel->loginCheck();
        $userRow = $userLoginModel->getLoginedRow();
        if ($loginStatus == 10) {
            exit('<html><body><script language="javascript">window.open("' . $this->view->projectUrl(array('controller'=>'login', 'action'=>'logout')) . '", "_top", "");</script></body></html>');
        } else if ($loginStatus == 20) {
            exit('<html><body><script language="javascript">alert("你的账户被锁定，如有问题请与管理员联系。");window.open("' . $this->view->projectUrl(array('controller'=>'login', 'action'=>'logout')) . '", "_top", "");</script></body></html>');
        } else if ($loginStatus == 30) {
            $cookieModel = new Application_Model_Cookie();
            exit('<html><body><script language="javascript">alert("你的账户已在IP为：' .$cookieModel->getCookie('adminUserIp'). '地方登录，你被系统强行退出。");window.open("' . $this->view->projectUrl(array('controller'=>'login', 'action'=>'logout')) . '", "_top", "");</script></body></html>');
        } else {
            if ($userRow['newUserIsResetPassword'] == 0) {
                echo '<html><body><script language="javascript">window.open("' . $this->view->projectUrl(array('controller'=>'login', 'action'=>'first-login-edit-password', 'userName' => $userRow['userName'])) . '", "_top", "");</script></body></html>';
                exit;
            }
            $this->_currentUserRow = $userLoginModel->getLoginedRow();
            $this->view->currentUserRow = $this->_currentUserRow;
        }

        //检查是否有菜单访问权限
        $menu = array('module'=>$this->_request->getModuleName(), 'controller'=>$this->_request->getControllerName(), 'action'=>$this->_request->getActionName());
        $menuIsAccess = $userLoginModel->menuIsAccess($menu, $this->_currentUserRow['id'], $userLoginModel->getUserCurrentRoleId(),$userLoginModel->getUserCurrentProductCode());

        if ($menuIsAccess === 0) {
            exit('权限不足，访问拒绝。');
        }

        //以下控制器限制配置的用户才可以访问
        if ($this->_request->getControllerName() == 'menu') {
            if (!in_array($this->_currentUserRow['userName'], $this->_configs['project']['admin']['master']['developer'])){
                exit('权限不足，访问拒绝（你不是开发人员）。');
            }
        }
        if ($this->_request->getControllerName() == 'product-menu') {
            if (!in_array($this->_currentUserRow['userName'], $this->_configs['project']['admin']['master']['developer']) && !in_array($this->_currentUserRow['userName'], $this->_configs['project']['admin']['master']['common'])){
                exit('权限不足，访问拒绝（你不是特别管理员）。');
            }
        }
    }
}